<template>
  <CommonPage :title="pageTitle">
    <template #mid>
      <SearchForm>
        <template #extraButtons>
          <a-button type="primary" @click="showCreateModal">
            {{ addText }}
          </a-button>
        </template>
      </SearchForm>
    </template>
    <SlTable :columns="columns" row-key="id" :data-source="tableData" :pagination="pagination" :loading="tableLoading">
      <template #bodyCell="{ column, record }: BodyCellOptions<Partial<ReceiptItem>>">
        <div v-if="column.key === 'oper'">
          <!-- <a class="mr-2" @click="addSubMenu(record)"> 新增下级 </a> -->
          <div>
            <a @click="showUpdateModal(record)">编辑</a>
            <a class="ml-2 text-red-400" @click="handleDelete(record)">删除</a>
          </div>
          <div v-if="record.status !== ReceiptStatus.disabled">
            <a v-if="record.status === ReceiptStatus.waiting" @click="handleExamine(record)">审核</a>
            <span v-if="record.status === ReceiptStatus.waiting" class="inline-block w-2"></span>
            <a class="text-red-400" @click="handleCancel(record)">作废</a>
          </div>
        </div>
        <StatusCell v-else-if="column.dataIndex === 'status'" :status="record.status" />
        <WidthFitCell
          v-else
          :title="column.title"
          :value="record[column.dataIndex as string]"
          :custom-render="column.customRender"
        />
      </template>
      <!-- v-else-if="
            ['orderQuantity', 'orderAmount', 'warehousingAmount', 'warehouseAdmin', 'warehousingQuantity', 'deliveryQuantity'].includes(
              column.dataIndex as string
            )
          " -->
    </SlTable>
    <ReceiptModal
      v-model:visible="wrVisible"
      :columns="columns"
      :title="wrTitle"
      :reload-table="reloadTable"
      :wr-id="curId"
      :receipt-type="receiptType"
    />
  </CommonPage>
</template>
<script lang="ts" setup>
import { ref, shallowRef } from "vue";
import { useI18n } from "/@/locales/useI18n";
import { useCURD } from "/@/hooks/useCURD";
import { message, type TableColumnProps } from "ant-design-vue";
import { PatternKey } from "/@/utils/verify";
import { loopItem } from "/@/utils/treeData";
import type { BodyCellOptions } from "/@/types/antd";
import {
  DeliveryType,
  ReceiptType,
  ReceiptItem,
  ReceiptStatus,
  ReceiptStatusDetail,
  ProfitLossType,
  AdjustmentType,
  PurchaseOrderType,
  GoodsSaleType,
} from "/@/api/main/pis/models";
import { Api, delWarehousingReceipt, wrNullify, wrReview } from "/@/api/main/pis";
import ReceiptModal from "./ReceiptModal.vue";
import dayjs from "dayjs";
import InfoModal from "/@/components/InfoModal";
import { getLabelByValueFromEnum } from "/@/utils/common";
import StatusCell from "./StatusCell.vue";
import { useReceiptManage } from "./useReceiptManage";

const props = defineProps<{
  receiptType: ReceiptType;
}>();

const { addText, pageTitle, editText, deleteText, innerColumns } = useReceiptManage(props.receiptType);

const columns = ref<TableColumnProps<Partial<ReceiptItem>>[]>(innerColumns);

const { pagination, tableData, tableLoading, curd, reloadTable } = useCURD<ReceiptItem>({
  getPageUrl: Api[props.receiptType + "ReceiptPage"],
});
const curId = ref();

const wrVisible = ref(false);
const wrTitle = ref("");
function showCreateModal() {
  curId.value = void 0;
  wrTitle.value = addText;
  wrVisible.value = true;
}
function showUpdateModal(record) {
  curId.value = record.id;
  wrTitle.value = editText;
  wrVisible.value = true;
}
function handleDelete(record) {
  InfoModal.warningConfirm({
    title: "提示",
    content: deleteText,
    async onOk() {
      const re = await delWarehousingReceipt(record.id, props.receiptType);
      if (re === undefined) {
        message.success("操作成功");
        reloadTable();
      }
    },
  });
}
function handleExamine(record) {
  InfoModal.warningConfirm({
    title: "提示",
    content: "确认审核通过吗？",
    async onOk() {
      const re = await wrReview(record.id, props.receiptType);
      if (re === undefined) {
        message.success("操作成功");
        reloadTable();
      }
    },
  });
}
function handleCancel(record) {
  InfoModal.warningConfirm({
    title: "提示",
    content: "确认作废吗？",
    async onOk() {
      const re = await wrNullify(record.id, props.receiptType);
      if (re === undefined) {
        message.success("操作成功");
        reloadTable();
      }
    },
  });
}
</script>
